package com.jtbd.service;

import com.jtbd.constants.ResponseCodeEnum;
import com.jtbd.domain.CourseClass;
import com.jtbd.dto.request.CourseClassItemHttpDto;
import com.jtbd.exceptions.SystemException;
import com.jtbd.handler.GetSecurityUserHandler;
import com.jtbd.service.campus.CourseClassService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class CourseClassManagementService {
    @Resource
    private CourseClassService courseClassService;

    /**
     * 添加课程班级
     *
     * @param courseClassHttp 课程班级对象，包含班级的基本信息
     * @return 返回添加成功的课程班级ID
     * @throws SystemException 当添加课程班级失败时抛出系统异常
     */
    public Long addCourseClass(CourseClassItemHttpDto courseClassHttp) {
        CourseClass courseClass = CourseClassItemHttpDto.toCourseClass(courseClassHttp);
        if (!ObjectUtils.isEmpty(courseClass)) {
            if (ObjectUtils.isEmpty(courseClass.getCreateId())) {
                Long userId = GetSecurityUserHandler.getSecurityUserId();
                courseClass.setCreateId(userId);
                courseClass.setUpdateId(userId);
            }
        }
        // 调用服务层添加课程班级，并获取返回的课程ID
        Long courseClassId = courseClassService.addCourseClass(courseClass);
        // 判断课程ID是否为空，如果不为空则返回课程ID
        if (!ObjectUtils.isEmpty(courseClassId)) {
            return courseClassId;
        }
        // 添加失败时抛出系统异常
        throw new SystemException(ResponseCodeEnum.ERROR.getCode(), "添加课程班级失败！");
    }

}
